//引入模块
import Api from './api.js'
import Cookie from './cookie.js'


new Vue({
    el:'#app',
    data(){
        return{
            userid:'',
            nc:'',
            passwd:''
        }
    },
    mounted(){

    },
    methods:{
        //注册
        async sign(){
            //判断输入是否合法
            let regExp=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if(!regExp.test(this.userid)){
                this.$message({
                    showClose: true,
                    message: '您的邮箱为空或输入不规范！',
                    type: 'error'
                });
            }else{
                regExp=/^[A-Za-z0-9\u4e00-\u9fa5]{1,10}$/;
                if(!regExp.test(this.nc)){
                    this.$message({
                        showClose: true,
                        message: '您的昵称为空或输入不规范！',
                        type: 'error'
                    });
                }else {
                    regExp = /^[A-Za-z0-9]{6,14}$/
                    if(!regExp.test(this.passwd)) {
                        this.$message({
                            showClose: true,
                            message: '您的密码为空或输入不规范！',
                            type: 'error'
                        });
                    }else {
                        //查询所有用户进行账号比较
                        let msg = await Api.login()
                        let flag=1
                        for (let i=0;i<msg.length;i++){
                            if(msg[i].zh==this.userid){
                                this.$message({
                                    showClose: true,
                                    message: '账号已存在！',
                                    type: 'error'
                                });
                                flag=null
                                break;
                            }
                        }
                        //账号没有重复进行注册
                        if (flag){
                            //调用注册接口
                            await Api.sign(this.userid,this.nc,this.passwd)
                            //存入cookie
                            //Cookie.setCookie("zh",this.userid,'s7200')
                            // Cookie.setCookie("nc",this.nc,'s7200')
                            // Cookie.setCookie("pwd",this.passwd,'s7200')
                            //存入session
                            sessionStorage.setItem("zh",this.userid)
                            this.$alert('<span>账号：'+this.userid+'</span><br><span>昵称：'+this.nc+'</span><br><span>密码：'+this.passwd+'</span><br>', '注册信息', {
                                dangerouslyUseHTMLString: true
                            }).then(action => {
                                    window.location.href='./mem.html'
                                });
                        }






                    }
                }
            }
        },

    }
})

